package org.apache.ibatis.binding;

import javax.sql.DataSource;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.domain.blog.Blog;
import org.apache.ibatis.domain.blog.Post;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Test;

public class WrongMapperTestMY {

	/**
	 * 直接采用默认构造方法创建的configuration来加载Mapper会失败！
	 * 因为缺少相关的属性。
	 */
	@Test(expected = BuilderException.class)
	public void test001(){
		
		Configuration configuration = new Configuration();
		configuration.addMapper(BoundBlogMapper.class);
		
	}
	
	/**
	 * 这是一个成功的范例。
	 * 提供了factory，datasource来构建configuration
	 * 
	 * 如果直接加载"BoundBlogMapper"也还是会失败，还要先注册"Blog", "Post", "Author"才行！
	 * 
	 * @throws Exception
	 */
	@Test
	public void test002() throws Exception{
		DataSource dataSource = BaseDataTest.createBlogDataSource();
	    TransactionFactory transactionFactory = new JdbcTransactionFactory();
	    Environment environment = new Environment("Production", transactionFactory, dataSource);
	    Configuration configuration = new Configuration(environment);
	    configuration.getTypeAliasRegistry().registerAlias(Blog.class);
	    configuration.getTypeAliasRegistry().registerAlias(Post.class);
	    configuration.getTypeAliasRegistry().registerAlias(Author.class);
	    configuration.addMapper(BoundBlogMapper.class);
	}
}
